﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zatify.Domain.Abstract;

namespace Zatify.Domain.Concrete
{
    public class ZipCode : IZipCode
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    }

    public class DistanceCalculator : IDistanceCalculator
    {
        private const double earthRadius = 3963;//3956.087107103049;

        public double Calculate(IZipCode zipCode1, IZipCode zipCode2)
        {
            if (zipCode1 == null || zipCode2 == null)
                throw new ArgumentException("One of the codes is null");

            double latitude1Radians = (zipCode1.Latitude / 180) * Math.PI;
            double longitude1Radians = (zipCode1.Longitude / 180) * Math.PI;

            double latitude2Radians = (zipCode2.Latitude / 180) * Math.PI;
            double longitude2Radians = (zipCode2.Longitude / 180) * Math.PI;

            //double distance = (earthRadius * 2) *
            //    Math.Asin(
            //    Math.Sqrt(
            //    Math.Pow(
            //    Math.Sin((latitude1Radians - latitude2Radians) / 2), 2) +
            //    Math.Cos(latitude1Radians) * Math.Cos(latitude2Radians) *
            //    Math.Pow(
            //    Math.Sin((longitude1Radians - longitude2Radians) / 2), 2)
            //    ));

            double distance = (3963.0 * Math.Acos(
                Math.Sin(zipCode1.Latitude / 57.295779513082323)
                    * Math.Sin(zipCode2.Latitude / 57.295779513082323)
                + Math.Cos(zipCode1.Latitude / 57.295779513082323)
                    * Math.Cos(zipCode2.Latitude / 57.295779513082323)
                    * Math.Cos((zipCode2.Longitude - zipCode1.Longitude) / 57.295779513082323)
                            ));

            return distance;
        }
    }
}
